package estateagent.rental.queryinformation_rental;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
 * 查询LendHouse表中的所有记录
 */
public class QueryInformationImpl {


	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rh = null;
		try {
			//1.加载驱动程序
			Class.forName("com.mysql.jdbc.Driver");
			//数据库的url
			String url =  "jdbc:mysql://202.196.37.168/EstateAgent?";
			//数据库用户名
			String user = "student";
			//数据库密码
			String password = "Student_123456";
			//2.获取到数据库的连接对象
			conn = DriverManager.getConnection(url, user, password);
			//3.获取语句对象
			stmt = conn.createStatement();
			//定义一个sql语句
			String sql = "select * from RentalHouse order by rhprice limit 10;";
			//4.语句对象将sql语句发送到数据库上,执行后得到结果集
			rh = stmt.executeQuery(sql);
			//5.遍历结果集,通过向下移动光标,光标指向哪一行就可以获取当前记录行的各个字段的值
			while(rh.next()){
				//获取当前记录行的各个字段的值,根据字段名获取字段值
				int rhno = rh.getInt("rhno");
				int rhsize =rh.getInt("rhsize");
				int rhfloor = rh.getInt("rhfloor");
				String rhtype = rh.getString("rhtype");
				String rharea = rh.getString("rharea");
				String rhdirection = rh.getString("rhdirection");
				String rhenviron = rh.getString("rhenviron");
				String rhlight = rh.getString("rhlight");
				String rhtraffic = rh.getString("rhtraffic");
				float rhprice = rh.getFloat("rhprice");
				//打印当前行各个字段值
				System.out.println("门牌号："+rhno + ";房屋面积："+rhsize+ ";楼层：" + 
				rhfloor+";类型："+rhtype+";地段："+rharea+";朝向："+rhdirection+";环境："+
						rhenviron+";采光："+rhlight+";交通："
				+rhtraffic+";价格："+rhprice);
			}
			
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//6.释放资源
			if(rh!=null){
				try {
					rh.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				rh = null;
			}
			
			if(stmt!=null){
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				stmt = null;
			}
			
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				conn = null;
			}
		}


	}


}
